package com.cf.message.controller;

import com.cf.constant.EnumReturnCode;
import com.cf.lock.core.ZLock;
import com.cf.message.service.PhoneCodeService;
import com.cf.vo.ResponseVo;
import com.cf.web.controller.CaptchaController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * boot-message-SmsController
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/7/25 下午 06:38
 */
@Api("短信发送接口")
@RestController
@RequestMapping("/zhs/sms")
public class SmsController implements CaptchaController {

    @Autowired
    private PhoneCodeService phoneCodeService;


    /**
     * @param key
     * @param phone
     * @return
     */
    @ApiOperation(value = "发送短信验证码")
    @ZLock(key = "SMS_LOCK_", value = "#phone")
    @RequestMapping(value = "/sendLoginCode", method = {RequestMethod.GET})
    public ResponseVo<String> sendSms(String key, String imgCode, String phone) {
        if (!checkCode(key, imgCode)) {
            return ResponseVo.fail(EnumReturnCode.ERROR.getCode(), "图形验证码过期！");
        }
        remove(key);
        boolean sendResult = phoneCodeService.sendSmsCode(phone, "环球加盟", "SMS_140525501", 15 * 60);
        return sendResult ? ResponseVo.success("发送成功！", "发送成功！") : ResponseVo.fail(EnumReturnCode.ERROR.getCode(), "短信通道暂时关闭！");
    }


    @Override
    public int getLength() {
        return 4;
    }

    @Override
    public int getHeight() {
        return 60;
    }

    @Override
    public int getWidth() {
        return 400;
    }

    @Override
    public String imgKeyPrefix() {
        return "SMS_IMG_KEY_";
    }
}
